package com.fili.core.params;

import com.fili.core.Log;
import com.fili.gui.components.graph.DataGraphConfig;
import java.beans.*;

import java.io.*;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 19, 2006
 * Time: 10:36:59 PM
 */
public class DryParams implements Serializable
{
    private static DryParams _this; //reference to the singleton
    private static String path = null;

    private DataGraphConfig dataGraphConfig;
    private String userName;
    private String userKey;

    private String[] dataStorePaths;

    private String serialPort;
    private int serialSpeed;
    private int serialBits;
    private int serialParity;
    private int serialStopBits;

    private boolean guiMinimizeOnClose;
    private boolean guiConfirmClose;

    private long firstRun;


    public DataGraphConfig getDataGraphConfig()
    {
        return dataGraphConfig;
    }

    public void setDataGraphConfig(DataGraphConfig dataGraphConfig)
    {
        this.dataGraphConfig = dataGraphConfig;
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getUserKey()
    {
        return userKey;
    }

    public void setUserKey(String userKey)
    {
        this.userKey = userKey;
    }


    public String getSerialPort()
    {
        return serialPort;
    }

    public void setSerialPort(String serialPort)
    {
        this.serialPort = serialPort;
    }

    public int getSerialSpeed()
    {
        return serialSpeed;
    }

    public void setSerialSpeed(int serialSpeed)
    {
        this.serialSpeed = serialSpeed;
    }

    public int getSerialBits()
    {
        return serialBits;
    }

    public void setSerialBits(int serialBits)
    {
        this.serialBits = serialBits;
    }

    public int getSerialParity()
    {
        return serialParity;
    }

    public void setSerialParity(int serialParity)
    {
        this.serialParity = serialParity;
    }

    public int getSerialStopBits()
    {
        return serialStopBits;
    }

    public void setSerialStopBits(int serialStopBits)
    {
        this.serialStopBits = serialStopBits;
    }

    public boolean isGuiMinimizeOnClose()
    {
        return guiMinimizeOnClose;
    }

    public void setGuiMinimizeOnClose(boolean guiMinimizeOnClose)
    {
        this.guiMinimizeOnClose = guiMinimizeOnClose;
    }

    public boolean isGuiConfirmClose()
    {
        return guiConfirmClose;
    }

    public void setGuiConfirmClose(boolean guiConfirmClose)
    {
        this.guiConfirmClose = guiConfirmClose;
    }

    public long getFirstRun()
    {
        return firstRun;
    }

    //no instantiation possible
    private DryParams()
    {
        try
        {
            if (path == null)
                path = new File(".").getCanonicalPath() + "/params.dat";
        }
        catch (IOException e)
        {
            Log.writeLog(e);
            path = "./params.dat";
        }

        dataGraphConfig = new DataGraphConfig();
        firstRun = System.currentTimeMillis();
        userName = "Demo Version";
        userKey = "DEMO-VERSION";
        serialPort = "COM1";
        guiConfirmClose = true;
        guiMinimizeOnClose = true;

        dataStorePaths = new String[8];
    }

    //obtain the singleton
    public static DryParams getParams()
    {
        if (_this == null)
            _this = new DryParams();

        return _this;
    }

    public static boolean saveParams()
    {
        getParams();    //make sure the path is built
        try
        {
            ObjectOutputStream oos = new ObjectOutputStream(new ParamsOutputFilter(new FileOutputStream(path)));
            oos.writeObject(_this);
            oos.flush();
            oos.close();
        }
        catch (IOException e)
        {
            Log.writeLog(e);
            return false;
        }
        return true;
    }

    public static boolean loadParams()
    {
        getParams();    //make sure the path is built
        try
        {
            ObjectInputStream ois = new ObjectInputStream(new ParamsInputFilter(new FileInputStream(path)));
            _this = (DryParams) ois.readObject();
            ois.close();
        }
        catch (Exception e)
        {
            Log.writeLog(e);
            _this = new DryParams();
            return false;
        }
        return true;
    }

    public String getStorePath(int index)
    {
        if (DryConfig.getConfig().isDemoMode())
            return null;
        if ((index >= 0) && (index < DryConfig.getConfig().getEncodedPermission().nrUsc))
            return dataStorePaths[index];
        return null;
    }

    public void setStorePath(int index, String path)
    {
        if (DryConfig.getConfig().isDemoMode())
            return;
        if ((index >= 0) && (index < DryConfig.getConfig().getEncodedPermission().nrUsc))
            dataStorePaths[index] = path;
    }
}
